package lls.demo.image.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class ImageGenUtil {

    private static final String BG_IMG_PATH = ImageGenUtil.class.getResource("/bg.jpg").getPath();
    private static final String IMG_FORMAT = "png";
    private static final AtomicInteger IMAGE_INDEX = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
//        TaskUtil.runMultiTasks(5, "/Volumes/APFS/Downloads/pngs", ImageUtil::genImage);
        TaskUtil.runMultiTasks(1000, "D:\\cpz\\imgs", ImageGenUtil::genImage);
    }

    private static void genImage(String dirPath) {
        try {
            BufferedImage bgImg = ImageIO.read(new File(BG_IMG_PATH));
            int x = RandomUtil.getRoundRandomInt(100, 2160);
            int y = RandomUtil.getRoundRandomInt(100, 1600);
            int index = IMAGE_INDEX.addAndGet(1);
            BufferedImage dstImg = resizeImage(x, y, bgImg);
            File out = new File(dirPath + File.separator + index + "." + IMG_FORMAT);
            boolean succ = ImageIO.write(dstImg, IMG_FORMAT, out);
            System.err.printf("已生成第%d张图片? %s\n", index, succ);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static BufferedImage resizeImage(int x, int y, BufferedImage bfi) {
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }

}